package com.loe.test.dialog

import android.app.Activity
import android.view.ViewGroup
import com.loe.mvp.BaseDialog
import com.loe.mvp.ext_view.setDelayClickListener
import com.loe.mvp.ext_view.visible
import com.loe.test.R
import com.loe.ktui.util.KtColor
import com.loe.mvp.ext_java.delay
import com.loe.mvp.ext_view.html
import kotlinx.android.synthetic.main.dialog_double.view.*

/**
 * 提示dialog
 *
 * @author 章路顺
 * @since 2021/11/23-9:50
 */
class DoubleDialog(activity: Activity, private var msg: String = "") : BaseDialog(activity, R.layout.dialog_double)
{
    private var okListener: ((DoubleDialog) -> Unit)? = null
    private var cancelListener: (() -> Unit)? = null

    private var title = "提示"
    private var cancelText = "取消"
    private var okText = "确认"
    private var okColor: Int = KtColor.main

    override fun ViewGroup.onView()
    {
        setAnimation()

        textTitle.visible = title.isNotEmpty()
        textTitle.html = title
        textMsg.html = msg

        delay(2000) {
            textMsg.invalidate()
            textMsg.requestLayout()
        }

        buttonCancel.html = cancelText
        buttonOk.html = okText
        buttonOk.setTextColor(okColor)

        buttonOk.setDelayClickListener()
        {
            okListener?.invoke(this@DoubleDialog)
            dismiss()
        }
        buttonCancel.setDelayClickListener()
        {
            cancelListener?.invoke()
            dismiss()
        }
        dialog.setOnCancelListener()
        {
            cancelListener?.invoke()
            dismiss()
        }
    }

    fun title(s: String): DoubleDialog
    {
        title = s
        return this
    }

    fun msg(s: String): DoubleDialog
    {
        msg = s
        return this
    }

    fun okColor(color: Int): DoubleDialog
    {
        okColor = color
        return this
    }

    fun okText(s: String): DoubleDialog
    {
        okText = s
        return this
    }

    fun cancelText(s: String): DoubleDialog
    {
        cancelText = s
        return this
    }

    fun ok(on: (DoubleDialog) -> Unit): DoubleDialog
    {
        okListener = on
        return this
    }

    fun cancel(on: () -> Unit): DoubleDialog
    {
        cancelListener = on
        return this
    }

    fun cancelOutside(enable: Boolean): DoubleDialog
    {
        canceledOnTouchOutside= enable
        return this
    }

    fun show(on: (DoubleDialog) -> Unit)
    {
        okListener = on
        show()
    }
}